// Copyright 2010 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.ads.common.lib.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * A utility class for retrieving image datum.
 *
 * @author Adam Rogal
 */
public final class ImageUtils {

  /**
   * Default constructor.
   */
  public ImageUtils() {}

  /**
   * Gets the image data {@code byte[]} located in {@code fileName}.
   *
   * @param fileName the image file to load
   * @return the image data {@code byte[]} located in {@code fileName}
   * @throws IOException if the image could not be read
   */
  public byte[] getImageDataFromFile(String fileName) throws IOException {
    return getByteArrayFromStream(new BufferedInputStream(new FileInputStream(new File(fileName))));
  }

  /**
   * Gets the image data {@code byte[]} located at {@code url} or
   * {@code null} if the image could not be loaded.
   *
   * @param url the image URL to load
   * @return the image data {@code byte[]} located at {@code url}
   * @throws IOException if the image could not be read from the URL
   */
  public byte[] getImageDataFromUrl(String url) throws IOException {
    return getByteArrayFromStream(new BufferedInputStream(new URL(url).openStream()));
  }

  /**
   * Gets the byte array from the input stream containing the full data from
   * that stream.
   *
   * @param inputStream the {@code InputStream} to get the byte array from
   * @return a byte array containing all data from the input stream
   * @throws IOException if the stream cannot be read
   */
  private byte[] getByteArrayFromStream(InputStream inputStream) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try {
      int i = 0;
      while ((i = inputStream.read()) != -1) {
        outputStream.write(i);
      }
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException e) {
          throw e;
        }
      }

      if (outputStream != null) {
        try {
          outputStream.close();
        } catch (IOException e) {
          throw e;
        }
      }
    }

    return outputStream.toByteArray();
  }
}
